//
//  TCommodityInfoDAL.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/11/7.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "TCommodityInfoDAL.h"
#import "CSFMDBTool.h"

@implementation TCommodityInfoDAL


+ (TCommodityInfoModel *)modelWithDict:(NSDictionary *)dict {
    
    TCommodityInfoModel * model = [[TCommodityInfoModel alloc] init];
    model.cId = [dict[@"cId"] intValue];
    model.cCoNo = dict[@"cCoNo"];
    model.cName = dict[@"cName"];
    model.cCategory = dict[@"cCategory"]; // 商品分类（大类,中类,小类）
    model.cUnit = dict[@"cUnit"]; // 商品单位（大箱,中盒,小个）
    model.cUnitConversion = dict[@"cUnitConversion"]; //单位转换（*50,*10,*1）与单位对应，包含多少个下级单位
    model.cPriceType = dict[@"cPriceType"]; //价格类型（标准价、其他价）
    model.cPrice = dict[@"cPrice"]; // 商品单价 与商品单位对应，用英文逗号分隔（100.00,20.00,1.20）
    model.cCreateUserNo = dict[@"cCreateUserNo"];
    model.cCreateTime = dict[@"cCreateTime"];
    model.cRemark = dict[@"cRemark"];
    
    return model;
}

+ (NSArray<TCommodityInfoModel *> *)loadModelListForSaleVcWithUserNo:(NSString *)userNo businessNo:(NSString *)businessNo {
    
    NSString * sql = [NSString stringWithFormat:@"select * from tCommodityInfo where sSalesmanNo='%@' and sBusinessmanNo='%@'; ",userNo, businessNo];
    
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr) {
        NSLog(@"error: (loadDataForSaleVcWithUserNo)load TCommodityInfoModel failed!");
        return nil;
    }
    
    NSMutableArray<TCommodityInfoModel *> *modelList = [NSMutableArray<TCommodityInfoModel *> array];
    for (NSDictionary *dict in arr) {
        [modelList addObject:[self modelWithDict:dict]];
    }
    return modelList.copy;
}

+ (NSArray<TCommodityInfoModel *> *)loadModelListWithCategory:(NSString *)category {
    NSString * sql = [NSString stringWithFormat:@"select * from tCommodityInfo where cCategory='%@'; ", category];
    
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr) {
        NSLog(@"error: (loadDataWithCategory)load TCommodityInfoModel failed!");
        return nil;
    }
    
    NSMutableArray<TCommodityInfoModel *> *modelList = [NSMutableArray<TCommodityInfoModel *> array];
    for (NSDictionary *dict in arr) {
        [modelList addObject:[self modelWithDict:dict]];
    }
    return modelList.copy;
}

+ (TCommodityInfoModel *)loadModelWithCategory:(NSString *)category {
    NSString * sql = [NSString stringWithFormat:@"select * from tCommodityInfo where cCategory='%@' limit 0,1; ", category];
    
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr || arr.count == 0) {
        NSLog(@"error: (loadDataWithCategory)load TCommodityInfoModel failed!");
        return nil;
    }
    return [self modelWithDict:arr[0]];
}

+ (TCommodityInfoModel *)loadModelWithCommNo:(NSString *)commNo {
    NSString * sql = [NSString stringWithFormat:@"select * from tCommodityInfo where cCoNo='%@'; ", commNo];
    
    NSArray<NSDictionary *> *arr = [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
    if (!arr) {
        NSLog(@"error: loadModelWithCommNo failed!");
        return nil;
    }else if (arr.count == 0) {
        NSLog(@"attention: loadModelWithCommNo nothing!");
        return nil;
    }
    
    return [self modelWithDict:arr[0]];
}

@end
